package main

import (
	"fmt"
	"reflect"
)

type AddReq struct {
	A, B int
}

type AddRsp struct {
	C int
}

type FuncAStruct struct {
	name    string
	Ip      string
	Port    int
	Address map[string]string
	Gender  bool
}

func functionA(p interface{}) {
	var (
		setstr  string
		setint  int64
		setbool bool
	)
	pt := reflect.TypeOf(p)
	if pt.Kind() == reflect.String || pt.Kind() == reflect.Int || pt.Kind() == reflect.Int64 {
		fmt.Println(reflect.ValueOf(p))
	}
	if pt.Kind() != reflect.Ptr {
		return
	}
	pt = pt.Elem()
	if pt.Kind() != reflect.Struct {
		return
	}
	pv := reflect.ValueOf(p).Elem()

	for i := 0; i < pt.NumField(); i++ {
		pf := pv.Field(i)
		if !pf.CanSet() {
			continue
		}
		name := pt.Field(i).Name

		fmt.Println("name:", name)
		switch pf.Kind() {
		case reflect.String:
			//setstr = pf.String()
			pf.SetString("asd")
			setstr = pf.String()
		case reflect.Int, reflect.Int64:
			setint = pf.Int()
		case reflect.Bool:
			setbool = pf.Bool()
		case reflect.Struct:
		default:
			//do nothing here
		}
	}
	fmt.Println("setstr:", setstr)
	fmt.Println("setint:", setint)
	fmt.Println("setbool:", setbool)
}
func Register(t interface{}) {
	rt := reflect.TypeOf(t)
	if rt.Kind() == reflect.Ptr {
		fmt.Println("in Kind() == reflect.Ptr")
		rt = rt.Elem()
	}
	name := rt.PkgPath() + "/" + rt.Name()
	fmt.Println("rt-string:", rt.String())
	fmt.Println("rt-name:", rt.Name())
	fmt.Println("name:", name)
}

func main() {
	//Register(AddReq{})
	functionA(&FuncAStruct{
		name: "zhangsan",
		Ip:   "localhost",
		Port: 3306,
		Address: map[string]string{
			"hubei":    "wuhan",
			"shenzhen": "nanshan",
			"china":    "hubei",
		},
		Gender: true,
	})

	//	functionA("abcdefg")
	//	functionA(int64(12))
}
